package com.wontube.smr.web.controller.i18n;

import java.io.IOException;
import java.util.ArrayList;
import java.util.List;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.servlet.ModelAndView;

import com.google.gson.Gson;
import com.wontube.client.modle.vo.PagenateInfo;
import com.wontube.client.remote.Remoting;
import com.wontube.smr.web.client.IBlogHandler;
import com.wontube.smr.web.client.model.po.Blog;
import com.wontube.smr.web.client.model.po.BlogCategory;
import com.wontube.smr.web.client.model.po.BlogTag;
import com.wontube.smr.web.client.model.po.TagCloud;
import com.wontube.smr.web.client.model.po.tagcloud.Html;
import com.wontube.smr.web.client.model.po.tagcloud.Link;
import com.wontube.smr.web.client.model.vo.BlogItems;
import com.wontube.smr.web.client.model.vo.TagCloudItems;

/**
 * 博客控制器
 * 
 * @author luobiao@wondershare.cn
 * 
 */
@Controller
@RequestMapping("/world")
public class I18nBlogController {
	private final static int BLOG_PAGE_SIZE = 8;
	private final static int RENCT_BLOG_PAGE_SIZE = 10;
	private final static int TAG_PAGE_SIZE = 30;
	
	@RequestMapping("/blog")
	public ModelAndView blog_index() {
		return list_blogs(1);
	}

	@RequestMapping("/blogs/{page}")
	public ModelAndView list_blog_page(@PathVariable("page") int page) {
		return list_blogs(page);
	}

	@RequestMapping("/blog/{urlTitle}")
	public ModelAndView getBlog(@PathVariable("urlTitle") String urlTitle, HttpServletResponse response)
			throws IOException {
		IBlogHandler handler = getBlogHanler();
		Blog blog = handler.getBlog(urlTitle);
		if (blog != null) {
			ModelAndView mv = new ModelAndView("i18n/blog/blog_content");

			List<BlogCategory> categories = handler.listAllBlogCategories();
			BlogItems recentItems = handler.listBlogs(new PagenateInfo(1, RENCT_BLOG_PAGE_SIZE));
			List<BlogTag> tags = handler.listAllBlogTags(new PagenateInfo(1, TAG_PAGE_SIZE));
			mv.addObject("recentItems", recentItems);
			mv.addObject("blog", blog);
			mv.addObject("categories", categories);
			mv.addObject("tags", tags);
			return mv;
		} else {
			response.sendError(HttpServletResponse.SC_NOT_FOUND);
			return null;
		}
	}

	@RequestMapping("/blog/{type}/{typeName}")
	public ModelAndView getCategories(@PathVariable("type") String type, @PathVariable("typeName") String typeName,
			HttpServletResponse response) {
		ModelAndView mv = new ModelAndView("i18n/blog/blog_list/list_index");
		mv = type_blogs(1, type, typeName);
		return mv;
	}

	@RequestMapping("/blog/{type}/{typeName}/{page}")
	public ModelAndView getCategoriesPage(@PathVariable("type") String type, @PathVariable("typeName") String typeName,
			@PathVariable("page") int page, HttpServletResponse response) {
		ModelAndView mv = new ModelAndView("i18n/blog/blog_list/list_index");
		mv = type_blogs(page, type, typeName);
		return mv;
	}

	@RequestMapping("/blog/extend/tagcloud/json")
	@ResponseBody
	public String getTagCloud(HttpServletRequest request) {
		IBlogHandler handler = getBlogHanler();

		List<BlogTag> tags = handler.listAllBlogTags(new PagenateInfo(1, 20));
		TagCloudItems tagCloudItems = new TagCloudItems();
		List<TagCloud> list = new ArrayList<TagCloud>();
		for (BlogTag tag : tags) {
			TagCloud tagCloud = new TagCloud();
			tagCloud.setText(tag.getTagName());
			tagCloud.setWeight(Double.valueOf(tag.getSortIndex() == null ? 1 : tag.getSortIndex()) * 0.1);
			tagCloud.setLink(new Link(request.getContextPath() + "/blog/tag/" + tag.getTagName()));
			tagCloud.setHtml(new Html(tag.getTagName()));
			list.add(tagCloud);
		}
		tagCloudItems.setItems(list);
		// Gson str=
		Gson gson = new Gson();
		String json = gson.toJson(tagCloudItems, TagCloudItems.class);

		return json;
	}

	private ModelAndView type_blogs(int pageNo, String type, String typeName) {
		ModelAndView mv = new ModelAndView("i18n/blog/blog_list/list_index");
		IBlogHandler handler = getBlogHanler();
		BlogItems blogItems = handler.listTypeBlogs(new PagenateInfo(pageNo, BLOG_PAGE_SIZE), type, typeName);
		BlogItems recentItems = handler.listBlogs(new PagenateInfo(1, RENCT_BLOG_PAGE_SIZE));
		List<BlogTag> tags = handler.listAllBlogTags(new PagenateInfo(1, TAG_PAGE_SIZE));
		List<BlogCategory> categories = handler.listAllBlogCategories();
		mv.addObject("blogItems", blogItems);
		mv.addObject("recentItems", recentItems);
		mv.addObject("categories", categories);
		mv.addObject("typeName", typeName);
		mv.addObject("type", type);
		mv.addObject("tags", tags);

		return mv;
	}

	private ModelAndView list_blogs(int pageNo) {
		IBlogHandler handler = getBlogHanler();
		BlogItems blogItems = handler.listBlogs(new PagenateInfo(pageNo, BLOG_PAGE_SIZE));
		BlogItems recentItems = handler.listBlogs(new PagenateInfo(1, RENCT_BLOG_PAGE_SIZE));
		List<BlogTag> tags = handler.listAllBlogTags(new PagenateInfo(1, TAG_PAGE_SIZE));
		List<BlogCategory> categories = handler.listAllBlogCategories();
		ModelAndView mv = new ModelAndView("i18n/blog/blog_index");
		mv.addObject("blogItems", blogItems);
		mv.addObject("recentItems", recentItems);
		mv.addObject("categories", categories);
		mv.addObject("tags", tags);
		return mv;
	}

	private IBlogHandler getBlogHanler() {
		Remoting<IBlogHandler> remoting = new Remoting<IBlogHandler>();
		IBlogHandler handler = remoting.createService(IBlogHandler.class);
		return handler;
	}
}
